﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;
using Session;
using PrimaryKeyFunction;


public partial class Admin_Liquid : System.Web.UI.Page
{
    Service objService = new Service();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
        }
        else
        {
            string ExperimentId = (string)Session["ExperimentId"];

            if (ExperimentId != null)
            {
                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;
            }
            string id = Request.QueryString["id"];

            if (id != null)
            {
                fillLiquidCompositionDetails(Convert.ToInt32(id));
                txtLiquidComposId.Text = string.Empty;
                trLiquidCompTextboxes.Visible = true;
                trDgLiquidComposition.Visible = false;
            }


            fillDataGrid();
        }
    }
    private void fillDataGrid()
    {
         string ExpID = (string)Session["ExperimentId"];
         if (ExpID != null)
         {
             if (objService.GetLiquidComposition(ExpID).Tables[0].Rows.Count > 0)
             {
                 dgLiquidComposistion.DataSource = objService.GetLiquidComposition(ExpID);
                 dgLiquidComposistion.DataBind();
                 trLiquidCompTextboxes.Visible = false;
                 trDgLiquidComposition.Visible = true;
             }
             else
             {
                 trLiquidCompTextboxes.Visible = true;
                 trDgLiquidComposition.Visible = false;
             }
         }
         else
         {
             trLiquidCompTextboxes.Visible = true;
             trDgLiquidComposition.Visible = false;
         }
    }

    private void clear()
    {
        txtAqueous.Text = string.Empty;
        txtWater.Text = string.Empty;
        txtOil.Text = string.Empty;
    }

    
    protected void dgLiquid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        
    }
    protected void btnOilComponents_Click(object sender, EventArgs e)
    {
        Response.Redirect("OilComponents.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("OilUltimateAnalysis.aspx");
    }
    protected void btnOilPhysicalProperties_Click(object sender, EventArgs e)
    {
        Response.Redirect("OilPhysicalProperties.aspx");
    }

    protected void dgLiquidComposistion_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgLiquidComposistion.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillLiquidCompositionDetails(id);
        trLiquidCompTextboxes.Visible = true;
        trDgLiquidComposition.Visible = false;
        btnPrevious.Visible = false;
    }

    private void fillLiquidCompositionDetails(Int32 id)
    {
        DataSet ds = new DataSet();
        ds = objService.GetTableDetailsBasedonPK("liquidcomp", id);
        txtLiquidComposId.Text = ds.Tables[0].Rows[0]["id"].ToString();
        txtAqueous .Text = ds.Tables[0].Rows[0]["aqueous"].ToString();
        txtOil.Text = ds.Tables[0].Rows[0]["oil"].ToString();
        txtWater.Text = ds.Tables[0].Rows[0]["water"].ToString();
   }

    protected void dgLiquidComposistion_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgLiquidComposistion.PageIndex = e.NewPageIndex;
        fillDataGrid();
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (txtLiquidComposId.Text != string.Empty)
        {
            
            if (txtAqueous.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("liquidcomp", "aqueous", "null" , txtLiquidComposId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("liquidcomp", "aqueous", txtAqueous.Text, txtLiquidComposId.Text);
            }

            if (txtOil.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("liquidcomp", "oil", "null", txtLiquidComposId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("liquidcomp", "oil", txtOil.Text, txtLiquidComposId.Text);
            }

            if (txtWater.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("liquidcomp", "water", "null" , txtLiquidComposId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("liquidcomp", "water", txtWater.Text, txtLiquidComposId.Text);
            }
            clear();
            fillDataGrid();
        }
        else
        {
            string LiquidCompId = (string)Session["LiquidCompId"];

            if (LiquidCompId != null)
            {
                lblMessage.Text = "Oil Physical properties Saved and " + objService.UpdateTable("liquidcomp", "aqueous", txtAqueous.Text, LiquidCompId);
                lblMessage.Text = "Oil Physical properties Saved and " + objService.UpdateTable("liquidcomp", "oil", txtOil.Text, LiquidCompId);
                lblMessage.Text = "Oil Physical properties Saved and " + objService.UpdateTable("liquidcomp", "water", txtWater.Text, LiquidCompId);
                clear();
                fillDataGrid();

            }
            else
            {
                if ((string)Session["ExperimentId"] != null)
                {
                    string ExpID = (string)Session["ExperimentId"];

                    lblMessage.Text = objService.SaveLiquidComp(txtAqueous.Text, txtOil.Text, txtWater.Text, null, null, null, ExpID);
                    
                    clear();

                    string LiquidCompRecentId = objService.GetLastInsertedId("experiment_id", (string)Session["ExperimentId"], "liquidcomp");
                       // GetLiquidComposistionRecentId();

                    Session["LiquidCompId"] = LiquidCompRecentId;

                    fillDataGrid();

                }
                else
                {
                    string RefeId = (string)Session["ReferenceId"];

                    objService.SaveExperiment(null, null, null, null, null, null, null, null, null, null, null, RefeId, null);

                    string ExperimentId = objService.GetExperimentId("ref_fk", RefeId);

                    lblMessage.Text = objService.SaveLiquidComp(txtAqueous.Text, txtOil.Text, txtWater.Text, null, null, null, ExperimentId);

                    Session["ExperimentId"] = ExperimentId;

                    clear();

                    fillDataGrid();

                  }                             
            }

            string LiquidCompUpdatedId = (string)Session["LiquidCompId"];

            if ((string)Session["YieldId"] != null)
            {
                string YieldId = (string)Session["YieldId"];

                lblMessage.Text = "Liquid Compositions Saved and" + objService.UpdateYield("liquidComp_fk", LiquidCompUpdatedId, YieldId);

            }
            else
            {
                string ReferenceId = (string)Session["ReferenceId"];

                lblMessage.Text = "Liquid Compositions Saved and" + objService.SaveYield(null, null, null, null, null, LiquidCompUpdatedId,null, ReferenceId, (string)Session["ExperimentId"]);

                string YieldId = objService.GetYieldId("liquidComp_fk", LiquidCompId);

                Session["YieldId"] = YieldId;
            }
        }
    }
    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        Response.Redirect("Previous Pages/PreLiquidComposition.aspx");
    }
}
